/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:org.cesgroup.course.util</p>
 * <p>File:WordConverter.java</p>
 * <p>类更新历史信息</p>
 * @todo chanlong(陈龙) 创建于 Sep 12, 2019 10:16:50 AM
 */
package org.cesgroup.course.converter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.cesgroup.course.enumeration.FileType;
import org.cesgroup.course.interceptor.Interceptor;
import org.cesgroup.course.interceptor.Invocation;
import org.cesgroup.course.interceptor.annon.Intercepts;
import org.cesgroup.course.interceptor.annon.Signature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import fr.opensagres.poi.xwpf.converter.core.ImageManager;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date Sep 12, 2019 10:16:50 AM
 * @version 1.0.2019
 */
@Component
@Intercepts({@Signature(type = Converter.class, method = "process", args = {Object.class, String.class})})
public class WordDocxConverter implements Interceptor {

	/**
	 * @see org.cesgroup.course.interceptor.Interceptor#intercept(org.cesgroup.course.interceptor.Invocation)
	 * @author chanlong(陈龙)
	 * @date Sep 12, 2019 11:22:36 AM
	 */
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		Object[] args = invocation.getArgs();
		String type = (String)args[1];
		if (type.equals(FileType.docx.name())) {
			return process((byte[]) args[0]);
		} 
		
		Converter converter = (Converter) invocation.getTarget();
		return converter.process(args[0], type);
	}
	
	private Object process(final byte[] data) throws Throwable {
		String staticpath = locations + "/static/";
		String tmpdir = System.getProperty("java.io.tmpdir");
		String destdir = FileUtil.mkdir(staticpath + "/html/").getPath();
		
		File folder = new File(tmpdir);
		File tempfile = FileUtil.createTempFile(folder);
		File datafile = FileUtil.writeBytes(data, tempfile);
		File destfile = FileUtil.newFile(destdir+ "/" + RandomUtil.randomString(6) + ".html");
		
		XWPFDocument document = new XWPFDocument(new FileInputStream(datafile));
        XHTMLOptions options = XHTMLOptions.create();
        
        ImageManager imageManager = new ImageManager(new File(destdir), "image/");
        options.setImageManager(imageManager);
        
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(destfile), "utf-8");
        XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
        xhtmlConverter.convert(document, outputStreamWriter, options);
        return destfile.getPath();
	}
	
	@Value("${ces.resources.static-location}")
	private String locations;

	String getLocations() {
		return locations;
	}

	void setLocations(String locations) {
		this.locations = locations;
	}
}
